﻿using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get Group Student --

        /// <summary>
        /// This function shows list of Student.
        /// </summary>
        /// <returns></returns>
        public List<GroupLecturer> GetLecturerByGroupId(int groupId)
        {
            return DataContex != null && groupId > 0
                        ? DataContex.GroupLecturers.Where(m => m.IdGroups == groupId).ToList() : null;
        }

        public List<GroupLecturer> GetGroupByLecturerId(int lecturerId)
        {
            return DataContex != null && lecturerId > 0
                        ? DataContex.GroupLecturers.Where(m => m.IdLecturer == lecturerId).ToList() : null;
        }

        public List<GroupLecturer> GetGroupLecturerByMajorId(int major, int kidintern)
        {
            return DataContex != null && major > 0 && kidintern > 0
                        ? DataContex.GroupLecturers.Where(m => m.Group.IdMajor == major && m.Group.IdKindIntern == kidintern ).ToList()
                        : null;
        }

        public List<GroupLecturer> GetGroupNeedLecturer(int majorId, int kidintern)
        {
            return DataContex != null && majorId > 0 && kidintern > 0
                        ? DataContex.GroupLecturers.Where(m => m.Group.IdMajor == majorId && m.Group.IdKindIntern == kidintern && m.IdLecturer == 1).ToList()
                        : null;
        }
        #endregion       

        #region -- Add Group Student--

        /// <summary>
        /// 
        /// </summary>
        /// <param name="grouplecturer"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddGroupLecturerValidator(GroupLecturer grouplecturer, out string message)
        {
            if (DataContex.GroupLecturers.Any(f => f.IdGroups == grouplecturer.IdGroups && f.IdLecturer == grouplecturer.IdLecturer))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Giảng viên hướng dẫn nhóm");
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddGroupLecturer(GroupLecturer grouplecturer)
        {
            DataContex.GroupLecturers.AddObject(grouplecturer);
            DataContex.SaveChanges();
        }     

        public void AutoAddGroupLecturer(List<Group> groups)
        {
            foreach (var @group in groups)
            {
                var grouplecturer = new GroupLecturer
                                        {
                                            IdGroups = @group.Id,
                                            IdLecturer = 1
                                        };
                AddGroupLecturer(grouplecturer);
            }
        }

        #endregion

        #region -- Edit Group Student --


        public bool EditGroupLecturerValidator(GroupLecturer grouplecturer, out string message)
        {
            var id = grouplecturer.Id;

            if (DataContex.GroupLecturers.Any(f => f.IdGroups == grouplecturer.IdGroups && f.IdLecturer == grouplecturer.IdLecturer && f.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, "Giảng viên hướng dẫn nhóm");
                return false;
            }
            message = string.Empty;
            return true;
        }
     
        public bool EditGroupLecturer(GroupLecturer groupleturer)
        {
            var id = groupleturer.Id;
            var updateGroupLecturer = DataContex.GroupLecturers.SingleOrDefault(f => f.Id == id);

            if (updateGroupLecturer == null)
                return false;

            updateGroupLecturer.IdLecturer = groupleturer.IdLecturer;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete Group Lecturer --

        public bool DeleteGroupLecturerValidator(IEnumerable<GroupLecturer> groups, out string message)
        {
            const string intialize = "Giảng viên hướng dẫn: ";
            var itemsCannotIdentify = (from group1 in groups
                                       let id = group1.Id
                                       where !DataContex.GroupLecturers.Any(m => m.Id == id)
                                       select group1).Aggregate(intialize, (current, group1) => current + (group1.IdGroups + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }                 

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteGroupLecturer(List<GroupLecturer> groups)
        {
            for (var i = groups.Count - 1; i >= 0; i--)
            {
                var id = groups[i].Id;
                var deleteGroup = DataContex.GroupLecturers.Single(m => m.Id == id);
                DataContex.GroupLecturers.DeleteObject(deleteGroup);
            }
            DataContex.SaveChanges();
        } 
     
        #endregion

    }
}